-- TreeHouse
-- Created by cheny3 Feb/23/2016
-- 树屋格子

require "game/ui/form/dungeon/special/UITreeHouseSubmit"
require "game/ui/form/dungeon/special/UITreeHouseMain"

-- 初始化
function initTreeHouse(self)
    local classId = self.gridData.class_id;
    local treeHouse = self.gridData.tree_house;
    local pos = self.gridData:getPos();

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开界面
            local uiForm;
            if not treeHouse:isActive() then
                uiForm = UITreeHouseSubmit.create(classId, pos);
            else
                uiForm = UITreeHouseMain.create(treeHouse, pos);
            end
            UIMgr.getCurrentScene():addForm(uiForm);

            -- 音效
            AudioM.playFx("equip_unload");
        end
    end

    -- 建筑图标
    local iconName = TreeHouseM.query(classId, "icon");
    local iconPath = getLevelItemIconPath(iconName);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 待机光效
    monkeyEffect(self);

    self:updateCountDown();

    -- 图标偏移
    self.icon:setPositionY(0);
end

-- 处理猴子光效
function monkeyEffect(self)
    local effectNode = self.icon.monkeyNode;
    if not self.icon.monkeyNode then
        -- 没有节点创建一个
        effectNode = cc.Node:create();
        self.icon:addChild(effectNode);
        self.icon.monkeyNode = effectNode;
    end
    effectNode:removeAllChildren();

    local treeHouse = self.gridData.tree_house;
    if not treeHouse or not treeHouse:isActive() then
        -- 未激活，待机状态
        playEffect(effectNode, 1325, 80, 100);
        return;
    end

    -- 激活后的待机状态
    local preTreeHouseSubmit = ME.user.dbase:query("pre_tree_house_submit");
    local skillId = treeHouse:query("next_skill");
    if type(skillId) ~= "number" then
        skillId = TreeHouseM.querySkillByItemId(preTreeHouseSubmit[1], "skill_id");
    end

    -- 如果是紧箍，特殊处理
    if TreeHouseM.querySkill(skillId, "rule") == "up_power" then
        monkeyLockRing(self);
        return;
    end

    local classId = TreeHouseM.querySkill(skillId, "class_id");
    local iconPath = getTreeHouseItemIconPath(ItemM.query(classId, "icon"));

    local _, effect = playEffect(effectNode, 1322, 80, 100);
    local itemNode = findChildByName(effect, "throw_node/SpriteObject");
    itemNode:setTexture(iconPath);
end

-- 猴子攻击
function monkeyAttackEffect(self, skillId)
    local treeHouse = self.gridData.tree_house;
    if not treeHouse or not treeHouse:isActive() then
        -- 没激活
        return;
    end

    if TreeHouseM.querySkill(skillId, "rule") == "up_power" then
        -- 紧箍，这个特殊一点
        -- monkeyLockRing(self, true);
        monkeyEffect(self);
        return;
    end

    -- 物品图片
    local classId = TreeHouseM.querySkill(skillId, "class_id");
    local iconPath = getTreeHouseItemIconPath(ItemM.query(classId, "icon"));

    local effectNode = self.icon.monkeyNode;
    if not self.icon.monkeyNode then
        -- 没有节点创建一个
        effectNode = cc.Node:create();
        self.icon:addChild(effectNode);
        self.icon.monkeyNode = effectNode;
    end
    effectNode:removeAllChildren();

    local function frameEventFunc(frame)
        if frame.getEvent and frame:getEvent() == "pilor" then
            local name = frame:getNode():getName();
            local index = frame:getFrameIndex();

            -- 最后一帧，回调待机
            if index == 25 then
                monkeyEffect(self);
            end
        end
    end

    -- 播放攻击动画
    local _, effect = playEffect(effectNode, 1323, 60, 100, frameEventFunc);
    local itemNode = findChildByName(effect, "throw_node/SpriteObject");
    itemNode:setTexture(iconPath);

    -- 返回攻击动画的持续时间    
    return 25 * 0.03;
end

-- 紧箍动画
function monkeyLockRing(self, callback)
    local treeHouse = self.gridData.tree_house;
    if not treeHouse or not treeHouse:isActive() then
        -- 没激活
        return;
    end

    local effectNode = self.icon.monkeyNode;
    if not self.icon.monkeyNode then
        -- 没有节点创建一个
        effectNode = cc.Node:create();
        self.icon:addChild(effectNode);
        self.icon.monkeyNode = effectNode;
    end
    effectNode:removeAllChildren();

    local function frameEventFunc(frame)
        if frame.getEvent and frame:getEvent() == "pilor" then
            local name = frame:getNode():getName();
            local index = frame:getFrameIndex();

            -- 最后一帧，回调待机
            if index == 38 then
                monkeyEffect(self);
            end
        end
    end

    -- 紧箍动画
    if callback then
        playEffect(effectNode, 1324, 80, 100, frameEventFunc);
    else
        playEffect(effectNode, 1324, 80, 100);
    end
end


